package com.amap.api.col.s;

import android.os.SystemClock;
import android.text.TextUtils;
import com.amap.api.col.s.dz;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;

/* compiled from: BaseNetManager.java */
/* loaded from: classes.dex */
public class dt {
    public static int a = 0;
    public static String b = "";
    public static HashMap<String, String> c;
    public static HashMap<String, String> d;
    public static HashMap<String, String> e;
    private static dt f;

    /* compiled from: BaseNetManager.java */
    /* loaded from: classes.dex */
    public interface a {
        URLConnection a();
    }

    public dt() {
        bx.d();
    }

    public static dt a() {
        if (f == null) {
            f = new dt();
        }
        return f;
    }

    @Deprecated
    private static ea d(dz dzVar, boolean z) throws bv {
        d(dzVar);
        dzVar.a(z ? dz.c.HTTPS : dz.c.HTTP);
        ea eaVar = null;
        long j = 0;
        boolean z2 = false;
        if (b(dzVar)) {
            boolean c2 = c(dzVar);
            try {
                j = SystemClock.elapsedRealtime();
                eaVar = a(dzVar, a(dzVar, c2), c(dzVar, c2));
            } catch (bv e2) {
                if (e2.f() == 21 && dzVar.o() == dz.a.INTERRUPT_IO) {
                    throw e2;
                }
                if (!c2) {
                    throw e2;
                }
                z2 = true;
            }
        }
        if (eaVar == null || eaVar.a == null || eaVar.a.length <= 0) {
            try {
                return a(dzVar, b(dzVar, z2), a(dzVar, j));
            } catch (bv e3) {
                throw e3;
            }
        }
        return eaVar;
    }

    public static ea a(dz dzVar) throws bv {
        return d(dzVar, dzVar.r());
    }

    private static ea a(dz dzVar, dz.b bVar, int i) throws bv {
        try {
            d(dzVar);
            dzVar.a(bVar);
            dzVar.c(i);
            return new dw().b(dzVar);
        } catch (bv e2) {
            throw e2;
        } catch (Throwable th) {
            th.printStackTrace();
            throw new bv("未知的错误");
        }
    }

    public static dz.b a(dz dzVar, boolean z) {
        if (dzVar.o() == dz.a.FIX) {
            return dz.b.FIX_NONDEGRADE;
        }
        if (dzVar.o() == dz.a.SINGLE) {
            return dz.b.NEVER_GRADE;
        }
        return z ? dz.b.FIRST_NONDEGRADE : dz.b.NEVER_GRADE;
    }

    public static dz.b b(dz dzVar, boolean z) {
        return dzVar.o() == dz.a.FIX ? z ? dz.b.FIX_DEGRADE_BYERROR : dz.b.FIX_DEGRADE_ONLY : z ? dz.b.DEGRADE_BYERROR : dz.b.DEGRADE_ONLY;
    }

    public static boolean b(dz dzVar) throws bv {
        d(dzVar);
        try {
            String a2 = dzVar.a();
            if (TextUtils.isEmpty(a2)) {
                return false;
            }
            String host = new URL(a2).getHost();
            if (!TextUtils.isEmpty(dzVar.i())) {
                host = dzVar.i();
            }
            return bx.g(host);
        } catch (Throwable unused) {
            return true;
        }
    }

    public static boolean c(dz dzVar) throws bv {
        d(dzVar);
        if (b(dzVar)) {
            if (dzVar.b().equals(dzVar.a()) || dzVar.o() == dz.a.SINGLE) {
                return false;
            }
            return bx.h;
        }
        return true;
    }

    public static int c(dz dzVar, boolean z) {
        try {
            d(dzVar);
            int m = dzVar.m();
            int i = bx.e;
            if (dzVar.o() != dz.a.FIX) {
                if (dzVar.o() != dz.a.SINGLE && m >= i && z) {
                    return i;
                }
            }
            return m;
        } catch (Throwable unused) {
            return 5000;
        }
    }

    public static int a(dz dzVar, long j) {
        try {
            d(dzVar);
            long j2 = 0;
            if (j != 0) {
                j2 = SystemClock.elapsedRealtime() - j;
            }
            int m = dzVar.m();
            if (dzVar.o() != dz.a.FIX && dzVar.o() != dz.a.SINGLE) {
                long j3 = m;
                if (j2 < j3) {
                    long j4 = j3 - j2;
                    if (j4 >= 1000) {
                        return (int) j4;
                    }
                }
                return Math.min(1000, dzVar.m());
            }
            return m;
        } catch (Throwable unused) {
            return 5000;
        }
    }

    public static void d(dz dzVar) throws bv {
        if (dzVar == null) {
            throw new bv("requeust is null");
        }
        if (dzVar.b() == null || "".equals(dzVar.b())) {
            throw new bv("request url is empty");
        }
    }
}
